//
//  GHFromViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/23.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHFromViewController.h"
#import "GHFormTableViewCell.h"
@interface GHFromViewController ()
@property(nonatomic, strong) NSMutableArray<GHFormModel*>                     *dataSouce;
@property(nonatomic, strong) GHNoResultView                                         *noResultView;
@end

@implementation GHFromViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSouce = [NSMutableArray new];
    self.title = @"报单中心";
    [self configSubView];

}
#pragma mark --subView
- (void)configSubView{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendMyformsRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    //无数据view
    self.noResultView = [[GHNoResultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 350) title:@"暂无报单" buttonTitle:@"没有需要确认的哦" buttonHandle:^(NSString *titleString) {
        
    }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    GHFormModel * model = self.dataSouce[indexPath.section];
    if (model.status == 0) {
        return [GHFormTableViewCell cellHeight];
    }
    return [GHFormTableViewCell cellHeight] - 35;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    GHFormTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GHFormTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    __weak typeof(self) weakSelf =  self;
    cell.model = self.dataSouce[indexPath.section];
    [cell.cancleButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCancleWithIndexPath:indexPath];
    }];
    [cell.confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedConfimWithIndexPath:indexPath];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelectedCancleWithIndexPath:(NSIndexPath*)indexPath{
    GHFormModel * model = self.dataSouce[indexPath.section];
    [self sendCancleFormRequestWithModel:model];
}
- (void)onSelectedConfimWithIndexPath:(NSIndexPath*)indexPath{
    GHFormModel * model = self.dataSouce[indexPath.section];
    [self sendConrimFormRequestWithModel:model];
}
#pragma mark --NetWork Method
- (void)sendMyformsRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_reports"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [self.tableView.mj_header endRefreshing];
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSArray * dataArray = responseObject[@"data"];
                                                 for (NSDictionary * dataDic in dataArray) {
                                                     GHFormModel * model = [GHFormModel mj_objectWithKeyValues:dataDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView.mj_header endRefreshing];
                                                 //[SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
//取消报单
- (void)sendCancleFormRequestWithModel:(GHFormModel*)model{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [dic setObject:model.formID forKey:@"id"];
     [dic setObject:@"-2" forKey:@"type"];
    
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/confirm_reports"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"取消成功"];
                                                 [self.tableView.mj_header beginRefreshing];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendConrimFormRequestWithModel:(GHFormModel*)model{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [dic setObject:model.formID forKey:@"id"];
    [dic setObject:@"2" forKey:@"type"];
    
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/confirm_reports"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"确认成功"];
                                                 [self.tableView.mj_header beginRefreshing];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
